local max = achievements.GetValue( "Dukes Of Hazzard", "max", 0 )

local function OnGround( veh )
	local nearest = veh:NearestPoint( veh:GetPos() - Vector( 0, 0, veh:BoundingRadius() * 2 ) )
	local trace = util.TraceLine( { start = nearest, endpos = nearest - Vector( 0, 0, 50 ), filter = veh } )
	return trace.Hit
end

local target = 100 
local start, offGround = nil, false
local function Update()
	if ( max == target ) then timer.Destroy( "Achievement.DukesOfHazzard" ) return end
	
	local ply = LocalPlayer()
	if ( !ValidEntity( ply ) ) then return end
	
	local veh = ply:GetVehicle()
	if ( !ValidEntity( veh ) ) then start = nil return end
	
	// Check for jeep/jalopy.
	if ( veh:GetClass() != "prop_vehicle_jeep" ) then return end
	
	local pos = veh:GetPos()
	pos.z = 0
	
	if ( OnGround( veh ) ) then
		if ( offGround && start ) then
			local distance = math.Clamp( math.floor( ( pos - start ):Length() / 12 ), 0, target )
			if ( distance > max ) then
				max = distance
				achievements.SetValue( "Dukes Of Hazzard", "max", max )
				achievements.Update( "Dukes Of Hazzard", max / target, max .. "/" .. target )
			end
			offGround = false
		end
		start = pos
	else
		offGround = true
	end
end

timer.Create( "Achievement.DukesOfHazzard", 0.5, 0, Update )

achievements.Register( "Dukes Of Hazzard", "Jump 100 feet in a jeep/jalopy.", "achievements/airtime", max / target, max .. "/" .. target )